# 公共类：封装对页面操作方法，每个页面都会继承
from selenium import webdriver
from time import strftime
import logging
# 配置日志
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',filename='../logs/login.txt')
class Base:
    # 构造方法：打开浏览器，访问指定url
    def __init__(self):
        self.driver = webdriver.Chrome()
        self.driver.maximize_window()
        self.driver.get('http://192.168.88.100:8000/admin/')
    # 定位元素方法
    def base_find_element(self, loc):
        # 记录日志
        logging.info(f'定位了元素{loc}')
        return self.driver.find_element(*loc)
    # 输入方法（元素，输入的值）
    def base_input(self, loc, value):
        # logging.info(f'正在输入：{value}')
        # 找到元素，先清空，再输入
        el = self.base_find_element(loc)
        el.clear()
        el.send_keys(value)
    # 点击方法（元素）
    def base_click(self, loc):
        # logging.info(f'正在点击元素：{loc}')
        self.base_find_element(loc).click()
    # 获取文本方法（元素）
    def base_get_text(self, loc):
        logging.info(f'正在获取文本：{loc}')
        return self.base_find_element(loc).text
    # 截图方法
    def base_get_image(self):
        # logging.info('正在截图')
        self.driver.get_screenshot_as_file(f'../../images/{strftime("%Y_%m_%d_%H_%M_%S")}.png')
    # 关闭浏览器方法
    def base_close(self):
        # logging.info('正在关闭浏览器')
        self.driver.quit()

if __name__ == '__main__':
    from selenium.webdriver.common.by import By
    from time import sleep
    username = (By.ID, 'id_username')  # 用户名输入框
    password = (By.CSS_SELECTOR, '#id_password')  # 密码框
    login_button = (By.CSS_SELECTOR, '[type="submit"]')  # 登录按钮
    b = Base()
    # 往用户名输入框输入admin
    b.base_input(username, 'admin')
    b.base_input(password, '123456')
    b.base_click(login_button)

    sleep(3)
    b.base_close()